//
//  YRChildVC.m
//  YRControlViewController
//
//  Created by 张常兴 on 15/11/13.
//  Copyright © 2015年 小怪兽. All rights reserved.
//
#define FULL_WIDTH                  [[UIScreen mainScreen] bounds].size.width
#define FULL_HEIGHT                 (SCREEN_HEIGHT - ((SYSTEM_VERSION >= 7) ? 0 : STATUSBAR_HEIGHT))
#define NAVBAR_HEIGHT               (44.f + ((SYSTEM_VERSION >= 7) ? STATUSBAR_HEIGHT : 0))

#import "YRChildVC.h"
#import "UIButton+WebCache.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "RTLabel.h"
#import <CoreText/CoreText.h>

@interface YRChildVC ()<UIScrollViewDelegate,UIWebViewDelegate>{
    
    
    NSMutableArray*_imageArr;

    NSMutableArray*_buttonImageArr;
    
    NSMutableArray*_btnList;

}

@end

@implementation YRChildVC

-(instancetype)initWithArr:(NSArray*)arr type:(YRChildType)type dic:(NSDictionary*)dic{
    
        self = [super init];
        if (self) {
    
            _imageArr=[[NSMutableArray alloc] init];
            _dataDic = [[NSMutableDictionary alloc]init];
            _dataDic = dic;

            _imageArr=_dataDic[@"big_img"];
    
            _buttonImageArr=_dataDic[@"small_img"];
    
            _btnList=[[NSMutableArray alloc] init];
    
            _pictureArray = arr;
            
            _type = type;

            
        }
        return self;

    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self doHttp];
}

/*-----------------------根据不同类型区分不同网络请求,这里用颜色模拟----------------------------*/
- (void)doHttp {
    
    switch (_type) {
            
        case YRTradingInvest:{
            
            NSLog(@"%@",self.pictureArray);
            OptionModel * model = self.pictureArray[0];
            
            self.web =  [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-40-64-50)];
            self.web.delegate = self;
            self.web.scrollView.delegate = self;
            self.web.scrollView.scrollEnabled = NO;
            
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.url]];
            [self.web loadRequest:request];
            [self.view addSubview:self.web];
        }
            break;
        case YRTradingOrder:{
            
            CGFloat W=(SCREEN_WIDTH-60)/3.0;
            
            CGFloat H=15;
            
            int Num=(int)(_imageArr.count+3)/3;
            
            CGFloat ScrollHeight=Num*(W+15)+15;
            
            RTLabel*label=[[RTLabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-30, 100)];
            label.text =_dataDic[@"verify_remark"];
            
            self.auditScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-40-50)];
            
            self.auditScrollView.contentSize=CGSizeMake(SCREEN_WIDTH, ScrollHeight+label.optimumSize.height+15+20);
            self.auditScrollView.backgroundColor = [UIColor whiteColor];
            self.auditScrollView.delegate=self;
            self.auditScrollView.scrollEnabled = NO;
            [self.view addSubview:self.auditScrollView];
            
            
            
            
            [_auditScrollView addSubview:label];
            
            for (int i=0; i<_imageArr.count; i++) {
                
                
                UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
                
                
                button.frame=CGRectMake(15+(W+15)*(i%3), label.bottom+ H+(W+15)*(i/3), W, W);
                
                button.tag=i;
                
                
               // [button sd_setImageWithURL:[NSURL URLWithString:_buttonImageArr[i]] forState:0];
                
                 [button sd_setBackgroundImageWithURL:[NSURL URLWithString:_buttonImageArr[i]] forState:0];
               // [button setBackgroundColor:kColor_AppNavColor];
                
                [button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
                
                [_auditScrollView addSubview:button];
                
                [_btnList addObject:button];
                
            }
            

            
            
        
        }
            break;
        case YRCapitalBuffers:{
            
            NSLog(@"%@",self.pictureArray);
            OptionModel * model = self.pictureArray[2];
            
            self.web =  [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-40-64-50)];
            self.web.delegate = self;
            self.web.scrollView.delegate = self;
            self.web.scrollView.scrollEnabled = NO;
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.url]];
            [self.web loadRequest:request];
            [self.view addSubview:self.web];

        }
            break;
            
        case YRDeposits:{
            
            OptionModel * model = self.pictureArray[3];
            self.web =  [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-40-64-50)];

             self.web.delegate = self;
            self.web.scrollView.delegate = self;
            self.web.scrollView.scrollEnabled = NO;
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.url]];
            
            [self.web loadRequest:request];
            [self.view addSubview:self.web];
  

        }
            break;
            
        default:
            break;
    }

}


-(void)ButtonClick:(UIButton*)button{
    
    
    
    NSArray * myImageUrlArr = (NSArray*)_imageArr;
    
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity: [myImageUrlArr count] ];
    
    for (int i = 0; i < [myImageUrlArr count]; i++) {
        
        NSString * getImageStrUrl = [NSString stringWithFormat:@"%@", [myImageUrlArr objectAtIndex:i] ];
        
        MJPhoto *photo = [[MJPhoto alloc] init];
        
        photo.url = [NSURL URLWithString: getImageStrUrl]; // 图片路径
        
        if (i < _btnList.count) {
            
            photo.srcBtn = _btnList[i];
            
            
        }
        
        [photos addObject:photo];
        
    }
    
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    
    browser.currentPhotoIndex = button.tag; // 弹出相册时显示的第一张图片是？
    
    browser.photos = photos; // 设置所有的图片
    
    browser.titles=_dataDic[@"title"];
    
    [browser show];
    
    
    
    
}

//只要滚动了就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    //    NSLog(@"只要滚动了就会触发ContentOffset  x is  %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
    
    if (scrollView.contentOffset.y < -30) {
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"backTop" object:nil];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
